//
//  SpecialCell.swift
//  CompareBuying
//
//  Created by 唐先才 on 16/1/29.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class SpecialCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
    
    
    @IBOutlet weak var _iconView: UIImageView!
    var collectionView: UICollectionView?
    var _array: NSMutableArray = []
    var _model: TxcModel?
    var _listModel: ListRecommendInfoModel?

    override func awakeFromNib() {
        super.awakeFromNib()
        self.createUI()
    }
    /*
      应该将UIViewCollectionView封装起来
    */
    func createUI() {
        let headTap = UITapGestureRecognizer(target: self, action: "tapHead")
        _iconView.addGestureRecognizer(headTap)
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Horizontal
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 220, width: WIDTH, height: 130), collectionViewLayout: layout)
        guard let collectionView = collectionView else {
            print("创建collectionView失败")
            return
        }
        collectionView.backgroundColor = UIColor.whiteColor()
        self.contentView.addSubview(collectionView)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.showsHorizontalScrollIndicator = false
        let nib1 = UINib(nibName: "SpecialSmallCell", bundle: NSBundle.mainBundle())
        collectionView.registerNib(nib1, forCellWithReuseIdentifier: "specialSmallCell")
    }
    
    func configCellWithModel(model: TxcModel) {
        if let picture = model.strSpecialImage {
            _iconView.sd_setImageWithURL(NSURL(string: picture))
        }
        guard let listArray = model.listRecommendInfo else {
            return
        }
        _array.removeAllObjects()
        for dict in listArray {
            guard let dict = dict as? NSDictionary else {
                return
            }
            //这个解包耗时间比较多
            guard let listModel = ListRecommendInfoModel.modelWithDict(dict) as? ListRecommendInfoModel else {
                return
            }
            _array.addObject(listModel)
        }
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return _array.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if let specialSmallCell = collectionView.dequeueReusableCellWithReuseIdentifier("specialSmallCell", forIndexPath: indexPath) as? SpecialSmallCell {
            if self._array.count > 0 {
                if let listModel = self._array[indexPath.item] as? ListRecommendInfoModel {
                    specialSmallCell.configCellWithListModel(listModel)
                }
            }
            return specialSmallCell
        }
        return UICollectionViewCell()
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(110, 130)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
    
    func tapHead(tap: UITapGestureRecognizer) {
    
    }

}